<?php

namespace app\controller\user\controller;

use app\popo\dto\AddressBookDTO;
use app\popo\model\AddressBook;
use app\service\AddressBookService;
use app\support\ResponseHelper;
use DI\Attribute\Inject;
use Shayvmo\WebmanAnnotations\Annotations\DeleteMapping;
use Shayvmo\WebmanAnnotations\Annotations\GetMapping;
use Shayvmo\WebmanAnnotations\Annotations\PostMapping;
use Shayvmo\WebmanAnnotations\Annotations\PutMapping;
use support\Response;
use Webman\Http\Request;


class AddressBookController
{
    #[Inject]
    protected AddressBookService $addressBookService;

    /**
     * 查询默认地址
     * @return Response
     */
    #[GetMapping("/user/addressBook/default")]
    public function getDefault(): Response
    {
        $addressBook = new AddressBook();
        $addressBook->is_default = 1;
        $addressBook->user_id = user_id();
        $list = $this->addressBookService->list($addressBook);

        if ($list && count($list) === 1) {
            return ResponseHelper::successJson($list[0]);
        }

        return ResponseHelper::failJson("没有查询到默认地址");
    }

    /**
     * 设置默认地址
     * @param Request $request
     * @return Response
     */
    #[PutMapping("/user/addressBook/default")]
    public function setDefault(Request $request): Response
    {
        $this->addressBookService->setDefault($request->input('id'));
        return ResponseHelper::successJson("", "设置默认地址成功");
    }


    /**
     * 查询当前登录用户的所有地址信息
     * @return Response
     */
    #[GetMapping("/user/addressBook/list")]
    public function list(): Response
    {
        $addressBook = new AddressBook();
        $addressBook->user_id = user_id();
        $list = $this->addressBookService->list($addressBook);
        return ResponseHelper::successJson($list);
    }

    /**
     * 新增地址
     * @param Request $request
     * @return Response
     */
    #[PostMapping("/user/addressBook")]
    public function save(Request $request): Response
    {
        $addressBookDTO = new AddressBookDTO($request);
        $this->addressBookService->save($addressBookDTO);
        return ResponseHelper::successJson("", "新增地址成功");
    }

    /**
     * 根据id查询地址
     * @param int $id
     * @return Response
     */
    #[GetMapping("/user/addressBook/{id}")]
    public function getById(int $id): Response
    {
        $addressBook = $this->addressBookService->getById($id);
        return ResponseHelper::successJson($addressBook);
    }

    /**
     * 根据id修改地址
     * @param Request $request
     * @return Response
     */
    #[PutMapping("/user/addressBook")]
    public function update(Request $request): Response
    {
        $addressBookDTO = new AddressBookDTO($request);
        $this->addressBookService->update($addressBookDTO);
        return ResponseHelper::successJson("", "修改地址成功");
    }


    /**
     * 根据id删除地址
     * @param Request $request
     * @return Response
     */
    #[DeleteMapping("/user/addressBook/")]
    public function deleteById(Request $request): Response
    {
        $id = (int)$request->input('id');
        $this->addressBookService->deleteById($id);
        return ResponseHelper::successJson("", "删除地址成功");
    }

}